package cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager;

import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.base.chat.ChatItem;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Item;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Section;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserChatContentDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserChatContentVo;
import cn.yitun.chat.yitunserverimbase.dao.userchat.UserChatContentDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.chat.function.ChatFunction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Date 2019-02-17 12:09:28<br>
 * Description 聊天相关的一些方法
 * 
 * @author yitun
 * @since 1.1
 */
@Service
public class ChatManager {

	@Resource
	private ChatFunction chatFunction;

	@Resource
	private UserChatContentDao userChatContentDao;


	//过滤聊天记录
	public List<ChatItem> wordsFilter(Content content) {
		content.setTimestamp(System.currentTimeMillis());
		List<ChatItem> chatItemList = new ArrayList<>();

		List<Section> sections = content.getSections();

		if (YitunCollectionUtil.isNotEmpty(sections)) {
			int sectionsSize = sections.size();
			for (int i = 0; i < sectionsSize; i++) {
				Section section = sections.get(i);
				List<Item> items = section.getItems();
				if (YitunCollectionUtil.isNotEmpty(items)) {
					int itemsSize = items.size();
					for (int j = 0; j < itemsSize; j++) {
						ChatItem chatItem = new ChatItem();

						Item item = items.get(j);
						String type = item.getType();
						String value = item.getValue();

						chatItem.setType(type);
						chatItem.setValue(value);
						chatItem.setSection(i);
						chatItem.setRank(j);

						if (Item.type_text.equals(type)) {
							value = chatFunction.wordsFilter(value);
							item.setValue(value);
							chatItem.setFilterValue(value);
						} else {
							chatItem.setFilterValue(value);
						}
						chatItemList.add(chatItem);
					}
				}
			}
		}
		return chatItemList;
	}

	/**
	 * 用户聊天记录 计数
	 * @param query
	 * @return
	 */
    public int queryUserChatCount(UserChatContentDto query) {
		return userChatContentDao.count(YitunMapUtil.java2Map(query, false));
    }

	/**
	 * 聊天记录 列表
	 * @param query
	 * @return
	 */
	public List<UserChatContentVo> queryUserChatList(UserChatContentDto query) {
		return userChatContentDao.listVO(YitunMapUtil.java2Map(query, false));
	}

	/**
	 * 聊天记录 详情
	 * @param id
	 * @return
	 */
	public UserChatContentVo getUserChatContent(Long id) {
		return userChatContentDao.getVO(id);
	}

	/**
	 * 详情列表
	 * @param query
	 * @return
	 */
	public List<UserChatContentVo> listUserChatContent(UserChatContentDto query) {

		return userChatContentDao.listVO(YitunMapUtil.java2Map(query, false));
	}

	/**
	 * 通过查询方向和点获取消息记录
	 * @param query
	 * @return
	 */
	public List<UserChatContentVo> getUserChatListByStartId(UserChatContentDto query) {
		return userChatContentDao.listVO(YitunMapUtil.java2Map(query, false));
	}

	/**
	 * 通过查询方向和消息key获取消息记录
	 * @param query
	 * @return
	 */
	public List<UserChatContentVo> getUserChatListByStartMessageKey(UserChatContentDto query) {
		return userChatContentDao.listVO(YitunMapUtil.java2Map(query, false));
	}
}
